/**
 * Copyright (C) 2021-2022 maminjie <canpool@163.com>
 * SPDX-License-Identifier: LGPL-2.1
 * Powered by Qt-Advanced-Docking-System
**/

#pragma once

#include "qads_global.h"
#include "dockwidget.h"

#include <QWidget>

QT_BEGIN_NAMESPACE
class QXmlStreamWriter;
class QSplitter;
QT_END_NAMESPACE

QADS_BEGIN_NAMESPACE

class DockPannel;
class DockWidget;
class DockManager;
class DockManagerPrivate;
class DockFloatingWidget;
class DockFloatingWidgetPrivate;
class DockFloatingDragPreview;
class DockStateReader;
class DockFloatingDragPreviewPrivate;
class DockContainerPrivate;

class QADS_EXPORT DockContainer : public QWidget
{
    Q_OBJECT
public:
    DockContainer(DockManager *dockManager, QWidget *parent = nullptr);
    ~DockContainer() override;

    DockPannel *addDockWidget(DockWidgetArea area, DockWidget *dockWidget, DockPannel *pannel = nullptr);
    void removeDockWidget(DockWidget *dockWidget);
    bool hasTopLevelDockWidget() const;
    QList<DockWidget *> openedDockWidgets() const;

    DockPannel *dockPannelAt(const QPoint &globalPos) const;
    DockPannel *dockPannel(int index) const;
    QList<DockPannel *> openedDockPannels() const;
    bool hasOpenDockPannels() const;
    int dockPannelCount() const;
    int visibleDockPannelCount() const;
    void closeOtherPannels(DockPannel *keepOpenPannel);

    DockWidget::DockWidgetFeatures features() const;

    DockFloatingWidget *floatingWidget() const;
    bool isFloating() const;

    virtual unsigned int zOrderIndex() const;
    bool isFrontOf(DockContainer *other) const;

    void dumpLayout() const;

signals:
    void dockPannelsAdded();
    void dockPannelsRemoved();
    void dockPannelViewToggled(DockPannel *pannel, bool open);

protected:
    QSplitter *rootSplitter() const;
    void createRootSplitter();
    void updateSplitterHandles(QSplitter* splitter);

    DockPannel *lastAddedDockPannel(DockWidgetArea area) const;
    DockPannel *topLevelDockPannel() const;
    DockWidget *topLevelDockWidget() const;

    QList<DockWidget *> dockWidgets() const;

    void dropFloatingWidget(DockFloatingWidget *floatingWidget, const QPoint &targetPos);
    void dropWidget(QWidget *widget, DockWidgetArea dropArea, DockPannel *targetPannel);

    void addDockPannel(DockPannel *pannel, DockWidgetArea area = CenterDockWidgetArea);
    void removeDockPannel(DockPannel *pannel);

    void emitAndExit() const; // TODO rename

    void saveState(QXmlStreamWriter &stream) const;
    bool restoreState(DockStateReader &stream, bool testing);

protected:
    bool event(QEvent *event) override;

private:
    DockContainerPrivate *d;
    friend class DockContainerPrivate;
    friend class DockManager;
    friend class DockManagerPrivate;
    friend class DockPannel;
    friend class DockPannelPrivate;
    friend class DockFloatingWidget;
    friend class DockFloatingWidgetPrivate;
    friend class DockWidget;
    friend class DockFloatingDragPreview;
    friend class DockFloatingDragPreviewPrivate;
}; // class DockContainer

QADS_END_NAMESPACE
